home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 27 / CU Amiga Magazine's Super CD-ROM 27 (1998)(EMAP Images)(GB)[!][issue 1998-10].iso / GoodBye / JohnKennedy / February96.lha / MASTERCLASS / MASTERAPRIL.DOC < prev    next >
Encoding:
Text File  |  1996-02-01  |  6.5 KB  |  236 lines

  1. MasterClass - April
  2.  
  3.  
  4. "ARexx isn't a programming language, it's more a way of
  5. life". People who say this kind of thing clearly spend way
  6. too much time in front of their Amiga and don't get out
  7. enough. I firmly believe that ARexx is a programming
  8. language, and deserves to be treated as such. Namely, it
  9. deserves to be used.
  10.  
  11. What can you do with it? Write programs is the obvious
  12. answer, and by golly that's what we'll do. ARexx looks a
  13. little like the well-known programming language BASIC, but
  14. it is considerably more flexible. Not only does it include
  15. many powerful commands, but it can control other programs
  16. and be expanded to include support for, as an example,
  17. standard Windows and Menus. 
  18.  
  19. Last month we saw how to install ARexx, and run a simple
  20. program. This month we'll look at some more programming
  21. examples to get an idea as to the way in which ARexx works.
  22. First of all, let's examine the SAY command as it can be
  23. slightly more tricky than you might expect. The best way to
  24. discover its secrets is of course to enter the following
  25. program and execute it. To do this, create a text file which
  26. contains the listing, save it and then use "RX" followed by
  27. the filename. Do no simple type this into a Shell, as it
  28. won't work.
  29.  
  30.  
  31.  
  32. Example listing 1
  33.  
  34.  
  35. /* Say examples */
  36.  
  37. SAY Hello world
  38.  
  39. SAY "Hello World"
  40.  
  41. SAY 'Hello World'
  42.  
  43. SAY "Hello" "World"
  44.  
  45. SAY "Hello"||"World"
  46.  
  47. SAY "'Hello World'"
  48.  
  49.  
  50. When you run the program with RX you'll see something like
  51. this:
  52.  
  53. example1.iff
  54.  
  55. The first thing to note is that the program, like all ARexx
  56. programs, starts with a comment. If you leave the comment
  57. out the program won't run. Now we come to six different ways
  58. of putting text on the screen. The first method uses no form
  59. or quotes -- only text. You can see that ARexx displays it
  60. as upper case.
  61.  
  62. The subsequent examples use quote makes. You can use either
  63. single or double, as long as you are consistant: the first
  64. two quoted examples will produce indentical results. You can
  65. also see that splitting the words into two and adding quotes
  66. to each automatically adds a space. If you don't want a
  67. space, use the concatenate command ( || ) which joins the
  68. two text strings together without introducing any extra
  69. characters. Finally, if you want to include quotation marks,
  70. use a different sort -- imagine the outmost pair being
  71. removed.
  72.  
  73. With output mastered, we can now learn how to make programs
  74. more interactive my getting input from the user. The
  75. simplest way to do this is with the PULL command, which
  76. waits at the Shell for something to be entered.
  77.  
  78. Example listing 2
  79.  
  80. /* Pull examples */
  81.  
  82. SAY "Hello, what's your name?"
  83. PULL name
  84. SAY "Nice to meet you" name 
  85. SAY "How old are you?"
  86. PULL age
  87.  
  88. IF age > 28 
  89.     THEN SAY "Wow, that's old..."
  90.     ELSE SAY "That's not very old."
  91.  
  92. EXIT
  93.  
  94. example2a.iff example2b.iff
  95.  
  96.  
  97.  
  98. The first PULL takes in a text string and converts it into
  99. upper case (we don't ask for this to happen, it just does).
  100. The second PULL takes in a numeric value. This is no
  101. distinction between the text and number PULLs as far as we
  102. are concerned, it's up to ARexx to keep track of which is
  103. text and which is a number. The IF-THEN-ELSE statements do a
  104. little processing, and that is all there is to it.
  105.  
  106. Variables
  107.  
  108. We can't go much further without touching on the concept of
  109. variables. Variables are locations in the Amiga's memory
  110. which store specific values, and are referenced by a special
  111. name. For example, when we asked for your age using PULL,
  112. ARexx create a variable and stored a number in it. ARexx
  113. deals with variables in a very flexible way: when it comes
  114. across some text which doesn't mean anything to it, it
  115. assume it's a variable. If the variable hasn't been used
  116. before (that is, it hasn't been initialised) then it has as
  117. a value it's own name in upper case.
  118.  
  119. This is why our very first example,  SAY Hello world, did
  120. what it did. ARexx thought "Hello" and "World" were to
  121. variables and so printed them. As the default contents of a
  122. variable is it's own name in upper case, the program display
  123. "HELLO WORLD".
  124.  
  125. Now here is an example of how you can make variables which
  126. store more than one value. Lets say we need to store the
  127. names of five different types of fish, for the start of an
  128. excellent Fish Database program. We could use five separate
  129. variables, and get the names like this:
  130.  
  131. ..
  132. PULL first-fish
  133. PULL second-fish
  134. PULL third-fish
  135. PULL fourth-fish
  136. PULL five-fish
  137. ..
  138.  
  139. and so on. However, this would make the program rather
  140. lengthy. It would also be very hard to expand the program at
  141. a later date to include more than five fish.
  142.  
  143. A better way is to use a compound variable -- a variable
  144. which has a single base name, but stores different values
  145. depending on the associated name appended with a full-stop.
  146. Erm... here, look at this:
  147.  
  148.  
  149. Example listing 3
  150.  
  151. /* Variable examples */
  152.  
  153. SAY "Five fish please"
  154.  
  155. DO i = 1 TO 5
  156.     PULL fish.i 
  157. END
  158.  
  159. SAY "Here is the list..."
  160.  
  161. DO i=1 TO 5
  162.     SAY fish.i
  163.     IF fish.i = "HADDOCK" THEN SAY "Hah! I knew you would say Haddock"
  164. END
  165.  
  166. EXIT
  167.  
  168. When it is run, you should enter five fishy types. The
  169. program automatically creates and assigns variables called
  170. "fish.1", "fish.2", "fish.3" and so on because the ".i" is
  171. actually the loop counter with itself counts from 1 to 50.
  172. Notice the second part of the program goes through the fish
  173. names itself, looking for a match. Imagine how hard this
  174. would be if there were fifty fish to process...
  175.  
  176. example3.iff
  177.  
  178.  
  179. Don't think that you have to use numbers to reference the
  180. values which a compound variable can store. Here is an
  181. interesting little example listing. It creates a minature
  182. database of ages for four people, and then asks you to pick
  183. a name. 
  184.  
  185.  
  186. Example listing 4
  187.  
  188. /* More Variable examples */
  189.  
  190.  
  191. age. = "Unknown"
  192. age.john = 28
  193. age.brian = 15
  194. age.mary = 25
  195. age.anne = 34
  196.  
  197. SAY "Enter a name, please:"
  198. PULL name
  199.  
  200. IF age.name = 'Unknown'
  201.     THEN SAY "Sorry, I don't know " name
  202.     ELSE SAY "That person is" age.name "years old"
  203.  
  204. EXIT
  205.  
  206.  
  207. Of particular interest is the fact that when no extra
  208. reference is given, there is an automatic default value
  209. present (which we call "Unknown") and this can be tested
  210. for.
  211.  
  212. example4.iff
  213.  
  214.  
  215.  
  216. Next time...
  217.  
  218. Making use of ARexx with existing programs: creating your
  219. own user-defined Macros to do exactly what you want with
  220. minimal effort.
  221.  
  222.  
  223.  
  224.  
  225. Box out: On the CDROM
  226.  
  227. ** Lisa there is another LHA file in with this collection,
  228. please give it to Mat to un-arc and place on the CDROM **
  229.  
  230. You will find these listings included on the CDROM in the
  231. drawer called (**please ask Mat!**). You will also find some
  232. programs from previous ARexx Masterclass features: please
  233. read the text file before running them!
  234.  
  235.  
  236.